package com.ruoyi.repayment.controller;


import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.repayment.mapper.LoanMapper;
import com.ruoyi.repayment.pojo.Repayment;
import com.ruoyi.repayment.pojo.RepaymentDb;
import com.ruoyi.repayment.pojo.RepaymentPeriod;
import com.ruoyi.repayment.utils.AverageCapitalUtils;
import com.ruoyi.repayment.utils.RepaymentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/loan/sq")
public class RepayController {
    @Autowired
    LoanMapper loanMapper;


    @PostMapping("/tryRepayment")
    public Repayment add(
            @RequestBody RepaymentDb repaymentDb
    ) {
        Long loanRepaymentWay = repaymentDb.getLoanRepaymentWay();
        Repayment repayment = new Repayment();
        int loanPeriods = repaymentDb.getLoanPeriods();
        BeanUtils.copyProperties(repaymentDb, repayment);
        if (loanRepaymentWay == 1) {//等额本金
            //等额本金的每月偿还本金
            double perMonthPrincipal = AverageCapitalUtils.getPerMonthPrincipal(repaymentDb.getLoanMoney(), repaymentDb.getLoanPeriods());
            //等额本金的每月偿还利息
            Map<Integer, Double> perMonthInterest = AverageCapitalUtils.getPerMonthInterest(repaymentDb.getLoanMoney(), repaymentDb.getLoanRate(), repaymentDb.getLoanPeriods());
            //等额本金的每月偿还本金和利息
            Map<Integer, Double> perMonthPrincipalInterest = AverageCapitalUtils.getPerMonthPrincipalInterest(repaymentDb.getLoanMoney(), repaymentDb.getLoanRate(), repaymentDb.getLoanPeriods());
            //等额本金的总利息
            double interestCount = AverageCapitalUtils.getInterestCount(repaymentDb.getLoanMoney(), repaymentDb.getLoanRate(), repaymentDb.getLoanPeriods());
            List<RepaymentPeriod> repaymentPeriodList = new ArrayList<>();
            for (int i = 1; i <= loanPeriods; i++) {
                RepaymentPeriod repaymd = new RepaymentPeriod();
                repaymd.setNo(i);
                repaymd.setPrinl(perMonthPrincipal);
                repaymd.setIntere(perMonthInterest.get(i));
                repaymd.setPrincipalAnd(perMonthPrincipalInterest.get(i));
                repaymentPeriodList.add(repaymd);
            }
            repayment.setRepaymentList(repaymentPeriodList);
            repayment.setTotalInterest(interestCount);
            return repayment;
        }
        if (loanRepaymentWay == 2) {//等额本息
            //等额本息的每月偿还利息
            Map<Integer, BigDecimal> perMonthInterest = RepaymentUtil.getPerMonthInterest(repaymentDb.getLoanMoney(), repaymentDb.getLoanRate(), repaymentDb.getLoanPeriods());
            //额本息的每月偿还本金
            Map<Integer, BigDecimal> perMonthPrincipal = RepaymentUtil.getPerMonthPrincipal(repaymentDb.getLoanMoney(), repaymentDb.getLoanRate(), repaymentDb.getLoanPeriods());
            //等额本息的总利息
            double interestCount = RepaymentUtil.getInterestCount(repaymentDb.getLoanMoney(), repaymentDb.getLoanRate(), repaymentDb.getLoanPeriods());
            //等额本息计算获取还款方式为等额本息的每月偿还本金和利息
            double perMonthPrincipalInterest = RepaymentUtil.getPerMonthPrincipalInterest(repaymentDb.getLoanMoney(), repaymentDb.getLoanRate(), repaymentDb.getLoanPeriods());
            List<RepaymentPeriod> repaymentList = new ArrayList<>();
            for (int i = 1; i <= loanPeriods; i++) {
                RepaymentPeriod repaymentd = new RepaymentPeriod();
                repaymentd.setNo(i);
                repaymentd.setIntere(perMonthInterest.get(i).doubleValue());
                repaymentd.setPrinl(perMonthPrincipal.get(i).doubleValue());
                repaymentd.setPrincipalAnd(perMonthPrincipalInterest);
                repaymentList.add(repaymentd);
            }
            repayment.setRepaymentList(repaymentList);
            repayment.setTotalInterest(interestCount);
            return repayment;

        }
        return repayment;
    }

}
